/*
 * Authors: Alex Attar & Kevin El Hage
 * ID: 9765727
 * Date: 9/3/2012
 * 
 */

package taskManagerSystem.ViewSubsystem;
import javax.swing.table.*;

import java.util.Vector;

/*
 * The TableMod class is a necessary class for the function of Swing tables.
 * It simply contains the model of the table and directs interactions with the table.
 * It should not be considered a part of, nor related to, the system's actual model.
 */
public class TableMod extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private String[] colNames;
	private Class<?>[] colTypes;
	private Vector<String[]> tableData = new Vector<String[]>();
	private boolean tableEditable = true;
	
	/*
	 * The constructor creates the rows and gives them the class types.
	 * colNames and colTypes should be of the same length and respective
	 * elements should match.
	 */
	public TableMod(String[] columnNames, Class<?>[] columnTypes){
		colNames = columnNames;
		colTypes = new Class[colNames.length];
		for(int i=0; i!=colNames.length; i++){
			if(i<columnTypes.length){
				colTypes[i] = columnTypes[i];
			}
			else {
				colTypes[i] = Object.class;
			}
		}
	}
	
	/*
	 * addRow adds a row from a string or an empty row. 
	 */
	public void addRow(String[] data){
		tableData.add(data);
		fireTableDataChanged();
	}
	
	public void addRow(){
		tableData.add(new String[colNames.length]);
		fireTableDataChanged();
	}
	
	/*
	 * deleteRow removes the row at "index"
	 */
	public void deleteRow(int index){
		tableData.remove(index);
	}
	
	/*
	 * empty clears the table.
	 */
	public void empty(){
		tableData = new Vector<String[]>();
	}
	
	@Override
	/*
	 * getColumnClass returns the class of the column with index columnIndex.
	 */
	public Class<?> getColumnClass(int columnIndex) {
		if (columnIndex < 0 || columnIndex >= colTypes.length){
			return Object.class;
		}
		else {
			return colTypes[columnIndex];
		}
	}

	@Override
	/*
	 * getColumnCount returns the number of columns
	 */
	public int getColumnCount() {
		return colNames.length;
	}

	@Override
	/*
	 * getColumnName returns the name of the column and index columnIndex
	 */
	public String getColumnName(int columnIndex) {
		String name = colNames[columnIndex];
		return name;
	}

	@Override
	/*
	 * getRowCount returns the number of rows in the table.
	 */
	public int getRowCount() {
		return tableData.size();
	}

	@Override
	/*
	 * getValueAt returns the value in a given cell.
	 */
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (rowIndex >= 0 && rowIndex < tableData.size()
				&& columnIndex >= 0 && columnIndex < colNames.length){
			String[] row = tableData.get(rowIndex); 
			return row[columnIndex];
		}
		return null;
	}

	@Override
	/*
	 * isCellEditable returns whether or not a cell may be modified.
	 */
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return tableEditable;
	}

	@Override
	/*
	 * setValueAt sets the value of a cell.
	 */
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if (aValue != null && aValue.getClass() == getColumnClass(columnIndex)){
			String[] row = tableData.get(rowIndex);
			row[columnIndex] = aValue.toString();
			tableData.set(rowIndex, row);
			fireTableCellUpdated(rowIndex, columnIndex);
		}
	}
	
	
}
